// pages/dynamic/dynamic.js
// const app=getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current:0,
    lineHeight:'2',//行高
    thumbUpNumber:'',//点赞数量
    focusOnNumber:'',//关注人数
    publishContent:'',//发布内容
    videoShow:false,//视频显示
    imgShow:false,//图片显示
    textShow:false,//文字显示
    theThemeShow:false,//主体显示
    query: false, //判断是否有图片
    icon:'',
    messageList:[{
      id:'001',
      headerImg:'',
      name: '',
    }],
  },

  // 滑块切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },
  //点击切换
  swiperChange: function (e) {
    this.setData({
      currentTab: e.detail.current,
    })
  },

  //关注
  focusOn:function(res){
    var that=this;
    var id = res.currentTarget.dataset.id
    var name = res.currentTarget.dataset.name
    wx.request({
      url: '',
      data:{
        id: id,
        userName:'name'
      },
      method: "",
      header: {
        'content-type': 'application/json'
      },
      success: function (focusOnSuccess){
        wx.showToast({
          title: '关注成功！',
          icon: 'success',
          duration: 2000
        })
      }, fail: function (focusOnFail){
        wx.showToast({
          title: '关注失败！',
          duration: 2000,
          image:''
        })
      }
    })
  },

  //关闭
  shutDown: function (res){
    var id = res.currentTarget.dataset.id
    var name = res.currentTarget.dataset.name
    wx.showModal({
      content: '关闭不再看他/她',
      success(res) {
        if (res.confirm) {//确定
          wx.request({
            url: '',
            method:'',
            data:{
              id: id,
              userName: 'name'
            },
            header: {
              'content-type': 'application/json'
            },
            // 测试
            // success:function(res){
            //   console.log(res)
            // }, fail: function (res){
            //   console.log(res)
            // }
          })
        } else if (res.cancel) {//取消

        }
      }
    })
  },

  //分享
  onShareAppMessage: function (res) {
    var that = this;
    console.log("" + that.data.taskId)
    if (res.from == 'button') {
      return {
        title: '',
        path: '',
        success: function (res) {
          console.log("分享成功", res)
        }
      }
    }
  },

  //评论
  comments: function (share) {
    var id = share.currentTarget.dataset.id;
    var name = share.currentTarget.dataset.name;
    console.log(id)
    console.log(name)
    var thth=this;
    wx.navigateTo({
      url:"/pages/videos/videos?id="+id+"&name"+name
    })
  },

  //点赞
  giveLike: function (share) {
    var id = share.currentTarget.dataset.id;
    var name = share.currentTarget.dataset.name;
    var that=this;
    
  },

  //上传图片
  add:function(){
    var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        var icon = that.data.icon
        var query = that.data.query
        that.setData({
          icon: tempFilePaths,
          query:true
        })
      }
    })
  },
   //没有图片提交的提示框
   hint:function(){  
    wx.showModal({
      title: '提示',
      content: '请添加图片',
    })
  },
  //清除图片
  cencel:function(){
    var that = this
    var icon = that.data.icon
    var http = 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/%E6%B7%BB%E5%8A%A0%E6%96%87%E4%BB%B6%E5%A4%B9.png'
    var query = that.data.query
    that.setData({
      icon: http,
      query:false
    })
  },
  getWords(e){
    var text = this.data.text
    this.setData({
      text: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var focusOnNumber = this.data.focusOnNumber
    var publishContent=this.data.publishContent
    var icon = this.data.icon
    // 初始化页面
    this.setData({
      currentTab: 0,
    })
    //初始化图片效果
    var http = 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/icons/%E6%B7%BB%E5%8A%A0%E6%96%87%E4%BB%B6%E5%A4%B9.png'
    that.setData({
      icon:http
    })
    //网络请求-获取关注
    wx.request({
      url: 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/FocusOn.json',
      method:'GET',
      header:{
        'content-type': 'application/json'
      },
      success: function (successRequest){//成功回调
        //console.log(successRequest)//测试
        that.setData({
          focusOnNumber:successRequest.data
        })
        var arr = Object.keys(successRequest.data).length//获取对象的长度
        for(let i=1;i<=arr;i++){
          //console.log(successRequest.data[i].name)
          if (successRequest.data[i].name.length>=2) {
            that.setData({
              lineHeight:1.1
            })
          }
        }
      }, fail: function (failRequest){//失败回调
        wx.showModal({
          content: '网络开了小差，是否刷新？',
          success(res) {
            if (res.confirm) {
              //页面刷新
              const pages = getCurrentPages()
              const perpage = pages[pages.length - 1]
              perpage.onLoad()  
            } else if (res.cancel) {
              
            }
          }
        })
      }
    })

    //网络请求-发布的内容
    wx.request({
      url: 'https://wechat-applets.oss-cn-chengdu.aliyuncs.com/json/release.json',
      method:'GET',
      header:{
        'content-type': 'application/json'
      },
      success:function(res){
        //console.log(res.data)
        that.setData({
          publishContent:res.data
        })
      },fail:function(res){

      }
    })
  },
  //上传事件
  upload:function(){
    var that = this
    var json = {
      isPublic: that.data.open,
      note:that.data.text,
      taskId:that.data.taskId,
      userId: app.globalData.userId
    };
    //上传提示框
    var image = that.data.img[0]
    wx.showToast({
      title: '上传中...',
      icon: 'loading',
      duration: 21000
    })
    //上传
    wx.uploadFile({
      url:'',
      filePath:image,
      name: 'image',
      formData: json,
      success:function(e){
        if (e.data == 'success'){
          //上传成功弹窗
          wx.showToast({
            title: '成功',
            icon: 'succes',
            duration: 1000,
            mask: true
          })
        }else{
          //图片错误弹窗
          wx.showModal({
            title: '提示',
            content: e.data,
          })
          setTimeout(function () {
            wx.hideToast()
          }, 2000)
        }
      },
      //网络请求错误提示框
      fail:function(){
        wx.showModal({
          title: '提示',
          content: '网络请求超时！！',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})